import Common from "../Common";
import ListsItem from "../common/lists/ListsItem";
import Page from "../common/Page";

const {ccclass, property} = cc._decorator;

@ccclass
export default class DrawRecords extends Common {

    @property(cc.Node)
    backBtn: cc.Node;

    @property(cc.Node)
    content: cc.Node;


    @property(cc.Node)
    page: cc.Node;

    itemPrefab: cc.Prefab


    protected bonusPage: any = {
        current_page: 1,
        last_page: 1,
        per_page: 0,
        total: 0,
    }

    protected bonusLists: any[]

    start() {
        cc.director.preloadScene('hall')
        this.initEmitter()
        this.getDrawRecords(1, false)
    }

    /**
     * 事件监听
     */
    protected initEmitter() {
        this.backBtn.on(cc.Node.EventType.TOUCH_START, () => {
            this.onBtnClick()
            cc.director.loadScene('hall')
        }, this)
        this.page.getComponent(Page).setCallback((page) => {
            this.getDrawRecords(page, false)
        })
    }

    async getDrawRecords(page = 1, isAppend = true) {
        try {
            const data = await this.http.get('bonus/drawRecords', {page})
            const lists = data.data || []
            if (isAppend) {
                this.bonusLists.push(...lists)
            } else {
                this.bonusLists = lists
            }
            this.bonusPage.current_page = data.current_page
            this.bonusPage.last_page = data.last_page
            this.bonusPage.per_page = data.per_page
            this.bonusPage.total = data.total
            this.page.getComponent(Page).init(data.current_page, data.last_page)
            this.initListItem(lists, isAppend)
        } catch (e) {

        }
    }

    protected async initListItem(lists = [], isAppend = true) {
        if (!isAppend) {
            this.content.removeAllChildren()
        }
        const itemPrefab = await this.getItemPrefab();
        lists.forEach(item => {
            const itemNode = cc.instantiate(itemPrefab)
            let color = '#999999', title = '未开奖'
            if (item.status == 1) {
                color = '#5ACA00';
                title = '中奖了'
            } else if (item.status == -1) {
                color = '#fea060';
                title = '未中奖'
            }
            const data = [
                {label: '抽奖金额', value: item.amounts},
                {label: '抽奖时间', value: item.created_at},
            ]
            if (item.drew_at) {
                data.push({label: '开奖时间', value: item.drew_at})
            }
            if (item.status == 1) {
                data.push({label: '中奖金额', value: item.drew_amount})
            }
            itemNode.getComponent(ListsItem).init(title, data, item.sn, color)
            this.content.addChild(itemNode)
        })
    }

    async getItemPrefab() {
        if (!this.itemPrefab) {
            this.itemPrefab = await this.getPrefab('lists/item')
        }
        return this.itemPrefab
    }

}
